package com.svms.service.dml.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.svms.domain.dao.SvmsIndentMapper;
import com.svms.domain.dao.SvmsSpareStaticMapper;
import com.svms.domain.dao.SvmsVehicleSpareMapper;
import com.svms.model.SvmsIndent;
import com.svms.model.SvmsVehicleSpare;
import com.svms.model.SvmsVehicleSpareExample;
import com.svms.model.SvmsVehicleSpareExample.Criteria;
import com.svms.service.dml.iface.IndentDMLIF;
import com.svms.service.factory.SvmsServiceFactory;
import com.svms.service.mapper.IndentMapper;
import com.svms.service.mapper.VehicleSpareMapper;
import com.svms.service.util.NumberGeneratorUtils;
import com.svms.service.validators.IndentValidator;
import com.svms.service.vo.IndentVO;
import com.svms.service.vo.VehicleSpareVO;

public class IndentDML implements IndentDMLIF {

	private IndentValidator indentValidator;

	@Autowired
	private SvmsIndentMapper svmsIndentMapper;
	@Autowired
	private SvmsVehicleSpareMapper svmsVehicleSpareMapper;

	public IndentValidator getIndentValidator() {
		return indentValidator;
	}

	public void setIndentValidator(IndentValidator indentValidator) {
		this.indentValidator = indentValidator;
	}

	@Override
	public IndentVO insertNewIndent(IndentVO indent) {

		try {

			indent.setIndentId(String.valueOf(NumberGeneratorUtils
					.generateIndentId()));

			SvmsIndent svmsIndent = IndentMapper.serviceToDml(indent);

			svmsIndentMapper.insert(svmsIndent);

			if (indent.getBillItems() != null) {
				List<VehicleSpareVO> billItems = indent.getBillItems();
				for (VehicleSpareVO vehcileSpareVO : billItems) {
					vehcileSpareVO.setVehicleSpareId(String.valueOf(NumberGeneratorUtils.generateVechielSpareID()));
					SvmsVehicleSpare svmsVehicleSpare = VehicleSpareMapper
							.serviceToDml(vehcileSpareVO);
					svmsVehicleSpareMapper.insert(svmsVehicleSpare);
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return indent;

	}

	@Override
	public IndentVO editIndent(IndentVO indent) {

		try {
			System.out.println("zzz");
			SvmsIndent svmsIndent = IndentMapper.serviceToDml(indent);
			svmsIndentMapper.updateByPrimaryKey(svmsIndent);
			for (VehicleSpareVO vehicleSpare : indent.getBillItems()) {
				System.out.println("zzz");
				SvmsVehicleSpare svmsVehicleSpare = VehicleSpareMapper
						.serviceToDml(vehicleSpare);
				svmsVehicleSpareMapper.updateByPrimaryKey(svmsVehicleSpare);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return indent;
	}

	@Override
	public IndentVO deleteIndent(IndentVO indent) {

		svmsIndentMapper.deleteByPrimaryKey(Long.valueOf(indent.getIndentId()));
		SvmsVehicleSpareExample example = new SvmsVehicleSpareExample();
		Criteria criteria = example.createCriteria();
		criteria.andInvoiceNumberEqualTo(Long.valueOf(indent.getInvoiceNumber()));
		svmsVehicleSpareMapper.deleteByExample(example);

		return indent;
	}

	@Override
	public IndentVO getIndent(IndentVO indent) {
		IndentVO indentVOFromDB = null;
		try {
			SvmsIndent svmsIndent = svmsIndentMapper.selectByPrimaryKey(Long
					.valueOf(indent.getIndentId()));
			indentVOFromDB = IndentMapper.dmlToService(svmsIndent);

			SvmsVehicleSpareExample example = new SvmsVehicleSpareExample();
			Criteria criteria = example.createCriteria();
			criteria.andInvoiceNumberEqualTo(Long.valueOf(indent
					.getInvoiceNumber()));

			List<SvmsVehicleSpare> svmsVehicleSpares = svmsVehicleSpareMapper
					.selectByExample(example);
			List<VehicleSpareVO> vehicleSpareVOs = new ArrayList<VehicleSpareVO>();
			for (SvmsVehicleSpare svmsVehicleSpare : svmsVehicleSpares) {
				vehicleSpareVOs.add(VehicleSpareMapper
						.dmlToService(svmsVehicleSpare));
			}
			indentVOFromDB.setBillItems(vehicleSpareVOs);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return indentVOFromDB;
	}

	@Override
	public List<IndentVO> getIndentList() {
		List<IndentVO> indentsToService = null;
		try {
			List<SvmsIndent> indentsFromDB = svmsIndentMapper
					.getEntireTableData();
			List<SvmsVehicleSpare> vehicleSparesFromDB = svmsVehicleSpareMapper
					.getEntireTableData();

			indentsToService = new ArrayList<IndentVO>();

			for (SvmsIndent svmsIndent : indentsFromDB) {

				IndentVO indentVO = IndentMapper.dmlToService(svmsIndent);
				List<VehicleSpareVO> vehicleSpareVOs = new ArrayList<VehicleSpareVO>();

				for (SvmsVehicleSpare svmsVehicleSpare : vehicleSparesFromDB) {
					if (indentVO.getInvoiceNumber()
							.equals(String.valueOf(svmsVehicleSpare
									.getInvoiceNumber()))) {
						vehicleSpareVOs.add(VehicleSpareMapper
								.dmlToService(svmsVehicleSpare));
					}
				}
				indentVO.setBillItems(vehicleSpareVOs);
				indentsToService.add(indentVO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return indentsToService;
	}

}
